<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>店铺列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            clearable
            v-model="selectMsg.msg"
            @clear="selectShopList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="Select()"
            ></el-button> </el-input
        ></el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="shoplist" border>
        <el-table-column prop="id" label="Id"> </el-table-column>
        <el-table-column prop="merchantName" label="店铺名称">
        </el-table-column>
        <el-table-column prop="levelId" label="店铺等级">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.levelId == 1">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.levelId == 2"
              >二级</el-tag
            >
            <el-tag type="info" v-else-if="scope.row.levelId == 3">三级</el-tag>
            <el-tag type="warning" v-else-if="scope.row.levelId == 4"
              >四级</el-tag
            >
            <el-tag type="danger" v-else-if="scope.row.levelId == 5"
              >五级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="usersId" label="所属用户"></el-table-column>
        <el-table-column prop="remarks" label="备注"> </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEidtDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeShopInfo(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pager.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pager.total"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑店铺对话框 -->
    <el-dialog
      title="编辑店铺信息"
      :visible.sync="editshop"
      width="60%"
      @close="editshopclosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <!-- 店铺名称 -->
        <el-form-item label="店铺名称" prop="merchantName">
          <el-input v-model="editForm.merchantName"></el-input>
        </el-form-item>
        <!-- 店铺等级 -->
        <el-form-item label="店铺等级" prop="levelId">
          <el-radio
            v-model="editForm.levelId"
            label="1"
            @change="editchangeRadio()"
            >一级</el-radio
          >
          <el-radio
            v-model="editForm.levelId"
            label="2"
            @change="editchangeRadio()"
            >二级</el-radio
          >
          <el-radio
            v-model="editForm.levelId"
            label="3"
            @change="editchangeRadio()"
            >三级</el-radio
          >
          <el-radio
            v-model="editForm.levelId"
            label="4"
            @change="editchangeRadio()"
            >四级</el-radio
          >
          <el-radio
            v-model="editForm.levelId"
            label="5"
            @change="editchangeRadio()"
            >五级</el-radio
          >
        </el-form-item>
        <!-- 所属用户 -->
        <el-form-item label="所属用户" prop="usersId">
          <el-input v-model="editForm.usersId"></el-input>
        </el-form-item>
        <!-- 备注 -->
        <el-form-item label="备注" prop="remars">
          <el-input v-model="editForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editshop = false">取 消</el-button>
        <el-button type="primary" @click="editShop()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted () {
    this.getshopList()
  },
  data () {
    return {
      // 搜索框内容
      selectMsg: {
        msg: '',
        currentPage: '',
        pageSize: '',
        total: ''
      },
      // radio数据初始化
      radio: 1,
      // 商品表格数据源
      shoplist: [
      ],
      // 要操作的行
      currentIndex: 0,
      // 分页用到的数据
      pager: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      },
      // 控制编辑对话框显示和隐藏
      editshop: false,
      // 编辑表单获取到的数据绑定
      editForm: {
        merchantName: '',
        levelId: '',
        usersId: '',
        remarks: ''
      },
      // 编辑表单验证规则
      editFormRules: {
        merchantName: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
        levelId: [{ required: true, message: '请输入店铺等级', trigger: 'blur' }],
        usersId: [{ required: true, message: '请输入所属用户', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 查找按钮
    Select () {
      if (this.selectMsg.msg) {
        this.SelectShopname(this.selectMsg)
      } else {
        this.getshopList(this.pager)
      }
    },
    // 类型名查寻方法
    async SelectShopname (params) {
      console.log(params.msg)
      params.currentPage = 1
      params.pageSize = this.pager.pageSize
      params.total = this.pager.total
      const { data: res } = await this.$http.get('Merchant/select/', { params: params })
      console.log(res)
      this.shoplist = res.data
    },
    // 获取编辑表单radio选中值
    editchangeRadio () {
      console.log(this.editForm.levelId)
    },
    // 获取店铺列表
    async getshopList () {
      const { data: res } = await this.$http.get('Merchant', { params: this.pager })
      this.shoplist = res.data
      this.pager.total = res.count
    },
    Openstatus (row) {
      console.log(row)
    },
    // 删除店铺
    async removeShopInfo (id) {
      // 弹框询问用户是否确认删除用户
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 取消
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      await this.$http.delete(`Merchant/${id}`)
      this.getshopList()
    },
    handleSizeChange (newSize) {
      // pagesize改变触发
      this.pager.pageSize = newSize
      this.getshopList()
    },
    handleCurrentChange (newPage) {
      // 页码改变触发
      this.pager.currentPage = newPage
      this.getshopList()
    },
    // 展示编辑对话框
    showEidtDialog (row) {
      this.editshop = true
      this.editForm = row
      console.log(row)
      this.editForm.levelId = row.levelId + ''
    },
    // 编辑店铺方法
    async editShop () {
      console.log(this.editForm)
      this.editForm.levelId = parseInt(this.editForm.levelId)
      const { data: res } = await this.$http.put(`Merchant/${this.editForm.id}`, this.editForm)
      console.log(res)
      this.getshopList()
      this.editshop = false
    },
    // 监听编辑对话框关闭的事件
    editshopclosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 清空搜索框重新获取数据
    selectShopList () {
      this.getshopList()
    }
  }
}
</script>

<style scoped>
</style>
